/*
 * Copyright 2010 Kevin L'Huillier <klhuillier@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.lhuillier.pwsafe.io;


/**
 * Used for output of binary data to assist development.
 */
public class BinaryOutput {
    public void printFormatted(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            if (i % 16 == 0) {
                System.out.printf("%08x: ", i);
            }
            System.out.printf("%02x", bytes[i]);
            if (i % 2 == 1) {
                System.out.print(" ");
            }
            if (i % 16 == 15) {
                for (int j = i - 15; j <= i; j++) {
                    if (bytes[j] >= 0x20) {
                        System.out.print((char) bytes[j]);
                    } else {
                        System.out.print('.');
                    }
                }
                System.out.println();
            }
        }
    }
    
    public void print(byte[] bytes) {
        for (byte b : bytes) {
            System.out.printf("%02x", b);
        }
        System.out.print(" ");
        for (byte b : bytes) {
            if (b >= 0x20) {
                System.out.print((char) b);
            } else {
                System.out.print('.');
            }
        }
        System.out.println();
    }
}
